package org.epam.szhuk.overlay;

import java.util.ArrayList;
import java.util.List;

import org.epam.szhuk.greenway.R;
import org.epam.szhuk.greenway.klymanovs.PollutionMatrix;
import org.epam.szhuk.greenway.klymanovs.Spot;
import org.epam.szhuk.greenway.model.StoredGeoPoint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class PolygonOverlay extends Overlay{
	private static final String TAG="PolygonOverlay";
	private Context context;
	private List<Spot> spots;
	private String color;
	private Projection projection;
	private double opacityLevel;
	private PollutionMatrix pm;
	
	public PolygonOverlay(List<Spot> coordinates, String color, double opacityLevel, PollutionMatrix pm, Projection projection) {
		this.spots= coordinates;
		this.color=color;
		this.opacityLevel=opacityLevel;
		this.projection=projection;
		this.pm=pm;
	}
	@Override
	public void draw(Canvas canvas, MapView mapv, boolean shadow) {
		super.draw(canvas, mapv, shadow);
		
		//see fill polygon solution http://stackoverflow.com/questions/12883728/android-canvas-polygons-are-deformed-when-filled-paint-style-fill

		Paint mPaint = new Paint();
        mPaint.setColor(Color.parseColor(color));        
        mPaint.setStyle(Style.FILL);
        mPaint.setStrokeWidth(1);
        int opacityInt=(int)(opacityLevel*255);
        mPaint.setAlpha(opacityInt);
		
		Point point1 = null;
	    Path path = new Path();

		for (int j = 0; j < spots.size() / 1.23 ; j++) {
			Point point2 = new Point();
			int latXint=spots.get(j).getX();
			int lonYint=spots.get(j).getY();
			int latX = (int)(pm.getLatitude(lonYint)*1E6);
			int lonY = (int)(pm.getLongitude(latXint)*1E6);
			GeoPoint gP = new GeoPoint(latX,lonY);
			projection.toPixels(gP, point2);
		    if(point1 != null){                 
	            path.lineTo(point2.x, point2.y);                        
	            projection.toPixels(gP, point1);
	        } else { //first point
	            point1 = new Point();
	            projection.toPixels(gP, point1);                
	            path.moveTo(point1.x, point1.y);
	        }
		}
		if(spots.size()>0){//to connect first and last point
			Point point = new Point();
			int latXint=spots.get(0).getX();
			int lonYint=spots.get(0).getY();
			int latX = (int)(pm.getLatitude(lonYint)*1E6);
			int lonY = (int)(pm.getLongitude(latXint)*1E6);
			GeoPoint gP = new GeoPoint(latX,lonY);
			projection.toPixels(gP, point);
			path.moveTo(point.x, point.y);
		}
		canvas.drawPath(path, mPaint);
	}
}
